package org.jeecg.modules.demo.yy.Model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class BaiduWenxinYiYanToken {
    public static String getWenxinToken() throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        String apiKey = "643bL12aNhlZsTqKK9ARvOXQ";
        String secretKey = "GLmdKkncTN3qRX1HqLjWymriLvAEMk25";

        HttpPost post = new HttpPost("https://aip.baidubce.com/oauth/2.0/token?client_id="
                + apiKey + "&client_secret=" + secretKey + "&grant_type=client_credentials");
        post.addHeader("Content-Type", "application/json");
        post.addHeader("Accept", "application/json");
        CloseableHttpResponse response = httpClient.execute(post);

        String s = EntityUtils.toString(response.getEntity());
        JSONObject objects = JSONArray.parseObject(s);
        String token = objects.getString("access_token");
        return token;
    }


}
